



let Action = require('./action');

class SequenceAction extends Action{
    constructor(actions){
        super();
        if(actions.length < 2){
            throw new Error(`SequenceAction 至少需要两个action`);
        }
        this._actions = actions;
        this._actionIdx = 0;
    }

    isDone() {
        return this._actionIdx >= this._actions.length;
    }

    start() {
        super.start();
        this._actions.forEach(action=>{
            action.target = this.target;
        });
        this._actionIdx = 0;
        this._actions[this._actionIdx].start();
    }

    step(dt) {
        let curAction = this._actions[this._actionIdx];
        curAction.step(dt);

        if(curAction.isDone()){
            this._actionIdx++;
            if(this._actionIdx < this._actions.length){
                curAction = this._actions[this._actionIdx];
                curAction.start();
            }
        }
    }
}

module.exports = SequenceAction;
